#include "Stdafx.h"

#include "DepthStencilView.h"

#include "RenderDevice.h"
#include "Texture2D.h"
#include "Texture.h"

namespace WindGE
{
	DepthStencilView::DepthStencilView()
		: RenderView()
		, dsv_(nullptr)
	{
	}

	DepthStencilView::~DepthStencilView()
	{
		release();
	}

	bool DepthStencilView::init(RenderDevicePtr renderDevice, TexturePtr tex, Texture2DPtr texBuff)
	{
		tex_ = tex;
		tex_buff_ = texBuff;

		D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc;
		dsvDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
		dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
		dsvDesc.Texture2D.MipSlice = 0;
		dsvDesc.Flags = 0;

		HRESULT hr = renderDevice->device()->CreateDepthStencilView(texBuff->d3dptr(), &dsvDesc, &dsv_);
		if (FAILED(hr)) return false;

		return true;
	}

	void DepthStencilView::release()
	{
		safe_release(dsv_);

		if (tex_) tex_->release();
		if (tex_buff_) tex_buff_->release();
	}

}//end namespace WindGE


